<?php

class Flexical_Admin_Object_Builder
{
	private $map;
	private $info;

	public function __construct( $map, $dynamo_info )
	{
		$this->map = $map;
		$this->dynamo_info = $dynamo_info;
	}

	public function build( $source )
	{
		$object = $this->dynamo_info->new_instance();

		foreach ( $this->map as $key => $spec )
		{
			if ( isset( $source[ $key ] ) )
			{
				$property = $spec->name;
				$value = null;

				switch ( $spec->type )
				{
					case 'string':
						$value = stripslashes( $source[ $key ] );
						break;

					case 'int':
						$value = (int) $source[ $key ];
						break;

					case 'boolean':
						$value = ( $source[ $key ] == 'true' );
						break;

					default:
						throw new Exception( "Unknown conversion type: " . $spec->type );
				}

				eval( "\$object->$property = \$value;" );
			}
		}

		return $object;
	}
}

class Flexical_Admin_Object_Property_Spec
{
	public $name;
	public $type;

	public function __construct( $name, $type )
	{
		$this->name = $name;
		$this->type = $type;
	}
}

?>